package com.example.cushion.realize.frame;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewStub;

import com.example.cushion.R;
import com.example.cushion.base.BaseActivity;
import com.example.cushion.global.SpKey;
import com.example.cushion.realize.frame.main.MainFragment;
import com.example.cushion.realize.frame.menu.MenuFragment;
import com.example.cushion.realize.frame.splash.GuideFragment;
import com.example.cushion.realize.frame.splash.OnEndListener;
import com.example.cushion.realize.frame.splash.SplashFragment;

import org.itzheng.and.baseutils.common.LanguageUtils;
import org.itzheng.and.baseutils.common.SPUtils;
import org.itzheng.and.baseutils.log.LogHelper;
import org.itzheng.and.baseutils.ui.UIUtils;
import org.itzheng.net.http.helper.HttpFullHelper;
import org.itzheng.net.http.proxy.callback.impl.HttpRequestCallback;
import org.itzheng.utils.net.NetUtils;

import java.net.NetPermission;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * 主界面，包含splashFragment guideFragment mainFragment 等
 */
public class MainActivity extends BaseActivity {
    /**
     *
     */
    ViewStub vsMainContent;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setLanguage();
        showSplash();
        testUtils();
    }

    private void testUtils() {
        String getUrl = "http://v.juhe.cn/toutiao/index";
        Map<String, Object> params = new HashMap<>();
        params.put("key", "4c2fb4a2fe6938af1e5bed8dfe3eda88");
        params.put("type", "top");
        HttpFullHelper.getInstance().get(getUrl)//.addParam("type", "top").addParam("key", "4c2fb4a2fe6938af1e5bed8dfe3eda88")
                .addParams(params)
                .setHttpCallBack(new HttpRequestCallback() {
                    @Override
                    public void onSuccess(String response) {
                        Log.d(TAG, "onSuccess: " + response);
                    }

                    @Override
                    public void onError(String error, Exception e) {
                        Log.w(TAG, "onError: " + error);

                    }
                }).exec();
    }

    private void setLanguage() {
        String language = SPUtils.getString(SpKey.STRING_APP_LANGUAGE, null);
        if (!TextUtils.isEmpty(language)) {
            Locale locale = LanguageUtils.newLocale(language);
            LanguageUtils.setAppLanguage(UIUtils.getContext(), locale);
            LanguageUtils.setAppLanguage(this, locale);
            LogHelper.d(TAG, "language:" + locale);
        }
    }


    /**
     * 显示启动页
     */
    private void showSplash() {
        setHideActionBar(true);
        setTranslucentNavigation(true);
        setTranslucentStatus(true);
        setStatusBarDarkMode(true);
        setStatusBarColor(Color.TRANSPARENT);
        setHideNavigation(true);
        final android.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        final SplashFragment splashFragment = new SplashFragment();
        fragmentTransaction.replace(R.id.flFragmentContent, splashFragment);
        fragmentTransaction.commit();
        getWindow().getDecorView().post(new Runnable() {
            @Override
            public void run() {
                splashFragment.start();
            }
        });
        splashFragment.setOnEndListener(new OnEndListener() {
            @Override
            public void onEnd(Fragment fragment) {
                if (false) {
                    //提示更新
                } else {
                    showContent();
                }
                getFragmentManager().beginTransaction().remove(fragment).commit();
            }
        });
    }

    boolean isFirst = true;

    /**
     * 显示需要显示的内容，如引导页，主界面
     */
    private void showContent() {
        if (isFirst) {
            isFirst = false;
            showGuide();
        } else {
            showMain();
        }
    }

    MenuFragment menuFragment = MenuFragment.newFragment();
    MainFragment mainFragment = MainFragment.newFragment();
    DrawerLayout drawerLayout;

    private void showMain() {
        setHideNavigation(false);
        setTranslucentNavigation(false);
        setStatusBarDarkMode(true);
        setNavigationBarColor(getResources().getColor(R.color.colorPrimaryDark));
        vsMainContent = (ViewStub) findViewById(R.id.vsMainContent);
        if (vsMainContent != null) {
            //初始化main主界面的布局
            vsMainContent.inflate();
        }
        drawerLayout = (DrawerLayout) findViewById(R.id.dlMainDrawerLayout);
        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {

            }

            @Override
            public void onDrawerOpened(View drawerView) {

            }

            @Override
            public void onDrawerClosed(View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
//        //禁用侧拉
//        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
//        mainFragment.setOnIconClickListener(new MainFragment.OnIconClickListener() {
//            @Override
//            public void onIconClickListener(View view) {
//                //打开侧滑菜单
////                drawerLayout.openDrawer(Gravity.LEFT);
////                SPUtils.put(SpKey.BOOL_SCREEN_ORIENTATION, !SPUtils.getBoolean(SpKey.BOOL_SCREEN_ORIENTATION, false));
////                setScreenOrientation();
//            }
//        });

        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.flMainContent, mainFragment);
        fragmentTransaction.replace(R.id.flMenuContent, menuFragment);
        fragmentTransaction.commit();
    }


    /**
     * 显示引导界面
     */
    private void showGuide() {
        final android.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        final GuideFragment fragment = new GuideFragment();
        fragmentTransaction.replace(R.id.flFragmentContent, fragment);
        fragmentTransaction.commit();
        fragment.setOnEndListener(new OnEndListener() {
            @Override
            public void onEnd(android.app.Fragment f) {
                getFragmentManager().beginTransaction().remove(f).commit();
                showMain();
            }
        });
        //这个是为了模拟结束
        getWindow().getDecorView().post(new Runnable() {
            @Override
            public void run() {
                fragment.start();
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (drawerLayout != null && drawerLayout.isDrawerOpen(Gravity.LEFT)) {
            closeDrawerLayout();
            return;
        }
        if (mainFragment != null && mainFragment.onBackPressed()) {
            return;
        }
        super.onBackPressed();
    }

    /**
     * 关闭侧滑菜单
     */
    private void closeDrawerLayout() {
        if (drawerLayout != null && drawerLayout.isDrawerOpen(Gravity.LEFT)) {
            drawerLayout.closeDrawer(Gravity.LEFT);
        }
    }

    /**
     * 打开侧滑菜单
     */
    private void openDrawerLayout() {
        if (drawerLayout != null && !drawerLayout.isDrawerOpen(Gravity.LEFT)) {
            drawerLayout.openDrawer(Gravity.LEFT);
        }
    }
}
